<script type="text/javascript">
<!--
/*
Written by Massimo Di Pierro.

This is a minimalistic AJAX for web2py, for more sophisticated stuff
you should use Scriptacolous or some other AJAX toolkit. Anyway...

To use web2py_ajax.html just make sure you include 'web2py_ajax.html'
in your layout.html. This provides four functions:

popup(url)
collapse(id)
fade(id,value) // try value= +0.1 or -0.1
ajax(url,sources,taget) 
// will send sources[i].value to url and dispaly result in target
*/

function popup(url) {
  newwindow=window.open(url,'name','height=400,width=600');
  if (window.focus) {newwindow.focus()}
  return false;
}

function collapse(id) {
   if(this.document.getElementById(id).style.display=="none")
   (this.document.getElementById(id).style.display="block") ;
   else (this.document.getElementById(id).style.display="none") ;
}

function fade(id,value) {
  var object=document.getElementById(id);
  if(value<0 && !object.style.opacity) {
     this.document.getElementById(id).style.display="block";
     object.style.opacity=1;
  }
  if(value>0 && object.style.opacity>=1) object.style.opacity=1;
  else if(value<0 && object.style.opacity<=0) {
     this.document.getElementById(id).style.display="none";
     object.style.opacity=0;
  }
  else {
     this.document.getElementById(id).style.display="block";
     object.style.opacity-=-value;
     setTimeout(function(){fade(id,value);},100);
     return false;
  }
  return true;
}

function ajax(url,sources,target) {
  var req = null; 
  var ctarget=document.getElementById(target);
  window.status="connecting...";
  if(window.XMLHttpRequest)
     req = new XMLHttpRequest(); 
  else if (window.ActiveXObject)
     req  = new ActiveXObject("Microsoft.XMLHTTP");
  req.onreadystatechange = function() {
     window.status="waiting...";
     if(req.readyState == 4)
       if(req.status == 200) {
          window.status='Ajax Done!'
          try { 
             ctarget.innerHTML=req.responseText; 
             try {ctarget.scrollTop=ctarget.scrollHeight-ctarget.clientHeight;}
             catch(err) {}
          } catch(err) { ctarget.value=req.responseText; }
       } else alert("AJAX Error: " + req.status);
  }
  var query=""
  for(i=0; i<sources.length; i++) {
     if(i>0) query=query+"&";
     query=query+encodeURIComponent(sources[i])+"="+encodeURIComponent(document.getElementById(sources[i]).value);
  }
  req.open("POST",url,true); 
  req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); 
  req.send(query); 
} 
//-->
</script>